/**
 * @Title: LoginAccessActivity.java
 * @Package com.jiuyang.baoxian.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author ChangJianlong
 * @date 2014-12-9 上午11:01:24
 * @version V1.0
 */
package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.ETUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.ResourcesUtil;
import com.bxd.baodan.util.SaveMsgUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.StringUtil;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * @author ChangJianlong
 * @ClassName: LoginAccessActivity
 * @Description: 登录主界面
 * @date 2014-12-9 上午11:01:24
 */
public class LoginAccessActivity extends BaseActivity implements
        OnClickListener, PlatformActionListener {
    private EditText etAccount;
    private EditText etPassword;
    private LoginInfo loginInfo;
    private ImageView can_see_pwd;
    private boolean canSeePassword = false;
    private TextView findPassword;
    private RelativeLayout mRlLogin;
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_login_main);
        from = getIntent().getIntExtra("from", 0);
        initViews();
    }

    private void initViews() {
        loginInfo = LoginInfo.getInstance(this);
        findPassword = getTextView(R.id.find_password);
        can_see_pwd = getImageView(R.id.can_see_pwd);
        etAccount = getEditText(R.id.account_text);
        etPassword = getEditText(R.id.password_input);
        mRlLogin = getRelativeLayout(R.id.rl_login);

        can_see_pwd.setOnClickListener(this);
        findPassword.setOnClickListener(this);
        mRlLogin.setOnClickListener(this);
        setActionBarTitle(ResourcesUtil.getStringById(R.string.ab_title_login));
        if (!StringUtil.isEmpty(loginInfo.getAccount())) {
            etAccount.setText(loginInfo.getAccount());
            etPassword.requestFocus();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    ETUtil.showSoftKeyboard(LoginAccessActivity.this,
                            etPassword);
                }
            }, 2 * 200);
        } else {
            etAccount.requestFocus();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    ETUtil.showSoftKeyboard(LoginAccessActivity.this, etAccount);
                }
            }, 2 * 200);
        }
    }

    public void attemptLogin() {
        if (!ETUtil.isMobileNO(etAccount)) {
            etAccount.setError("输入的手机号码不合格");
            etAccount.requestFocus();
            return;
        }
        if (ETUtil.isNotNull(etAccount, etPassword)) {
            NetUtil netUtil = new NetUtil(this, JsonApi.LOGIN);
            netUtil.setParams("telphone", getString(etAccount));
            netUtil.setParams("password", getString(etPassword));
            netUtil.postRequest("正在登录", new RequestStringListener() {

                @Override
                public void onComplete(String result) {

                    App.getContext().initDefaultValues();

                    loginInfo.saveLoginInfo(getString(etAccount),
                            getString(etPassword));
                    loginInfo.saveUserInfo(JSONUtil.getData(result),
                            LoginAccessActivity.this);
                    SpUtil.getInstance().savaString(
                            SharedPreferanceKey.ACCOUNT,
                            loginInfo.getAccount());
                    UserItem user = LoginInfo.getInstance(
                            getApplicationContext()).getUser();
                    LoginInfo.getInstance(getApplicationContext())
                            .getUser().setPhone(getString(etAccount));
                    SaveMsgUtil.updateLocalInfo(user,
                            getApplicationContext());// 更新用户的登录资料到本地
                    LoginAccessActivity.this.sendBroadcast(new Intent(
                            Constant.ACTION_SUCCESS_LOGIN));
                    LoginAccessActivity.this.finish();
                    if (from != 1) {
                        openActivity(MainActivity.class);
                    }
                }
            });
        }
    }


    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.can_see_pwd:
                canSeePassword = !canSeePassword;
                if (canSeePassword) {
                    // 显示密码
                    can_see_pwd.setImageResource(R.drawable.see_pwd);
                    etPassword
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    // 隐藏密码
                    can_see_pwd.setImageResource(R.drawable.eyeclosed);
                    etPassword.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
            case R.id.find_password:
                openActivity(FindPasswordActivity.class);
                break;
            case R.id.rl_login:
                // 登录
                attemptLogin();
                break;

            default:
                break;
        }
    }

    @Override
    public void onCancel(Platform arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onError(Platform arg0, int arg1, Throwable arg2) {
        // TODO Auto-generated method stub

    }
}
